package com.jingdianjichi.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * DruidEncryptUtil 数据库加密util
 *
 * @author 86178
 * @version 2024/03/17 11:56
 **/
public class DruidEncryptUtil {
    private static String publicKey;

    private static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 加密
     * @param plainText
     * @return
     */
    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
//        System.out.println("encrypt:" + encrypt);
        return encrypt;
    }

    public static String decrypt(String plainText) throws Exception {
        String decrypt = ConfigTools.decrypt(privateKey, plainText);
        System.out.println("decrypt:" + decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encrypt1 = encrypt("root");
        String encrypt2 = encrypt("Wing1Q2W#E");
//        String encrypt3 = encrypt("10.29.27.100");
        System.out.println("encrypt1:" + encrypt1);
        System.out.println("encrypt2:" + encrypt2);
//        System.out.println("encrypt3:" + encrypt3);

    }
}

